/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.mailbox.Conversation;
/*     */ import com.zimbra.cs.mailbox.DeliveryContext;
/*     */ import com.zimbra.cs.mailbox.DeliveryOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem.CustomMetadata;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.mime.ParsedMessageOptions;
/*     */ import com.zimbra.cs.redolog.RedoException;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import com.zimbra.cs.store.Blob;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.zip.GZIPInputStream;
/*     */ import javax.activation.DataSource;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CreateMessage
/*     */   extends RedoableOp
/*     */   implements CreateCalendarItemPlayer, CreateCalendarItemRecorder
/*     */ {
/*     */   private static final long RECEIVED_DATE_UNSET = -1L;
/*     */   private static final byte MSGBODY_INLINE = 1;
/*     */   private static final byte MSGBODY_LINK = 2;
/*     */   private long mReceivedDate;
/*     */   private String mRcptEmail;
/*     */   private boolean mShared;
/*     */   private String mDigest;
/*     */   protected long mMsgSize;
/*     */   private int mMsgId;
/*     */   private int mFolderId;
/*     */   private int mConvId;
/*     */   private int mConvFirstMsgId;
/*     */   private List<Integer> mMergedConvIds;
/*     */   private int mFlags;
/*     */   private String[] mTags;
/*     */   private String mTagIds;
/*     */   private int mCalendarItemId;
/*  81 */   private String mCalendarItemPartStat = "NE";
/*     */   
/*     */   private boolean mNoICal;
/*     */   private MailItem.CustomMetadata mExtendedData;
/*     */   protected RedoableOpData mData;
/*     */   private byte mMsgBodyType;
/*     */   private String mPath;
/*     */   
/*     */   public CreateMessage()
/*     */   {
/*  91 */     super(MailboxOperation.CreateMessage);
/*  92 */     this.mShared = false;
/*  93 */     this.mMsgId = 0;
/*  94 */     this.mFolderId = 0;
/*  95 */     this.mConvId = 0;
/*  96 */     this.mConvFirstMsgId = 0;
/*  97 */     this.mMergedConvIds = Collections.emptyList();
/*  98 */     this.mFlags = 0;
/*  99 */     this.mMsgBodyType = 1;
/* 100 */     this.mNoICal = false;
/*     */   }
/*     */   
/*     */ 
/*     */   protected CreateMessage(int mailboxId, String rcptEmail, boolean shared, String digest, long msgSize, int folderId, boolean noICal, int flags, String[] tags)
/*     */   {
/* 106 */     this(mailboxId, rcptEmail, -1L, shared, digest, msgSize, folderId, noICal, flags, tags, null);
/*     */   }
/*     */   
/*     */ 
/*     */   public CreateMessage(int mailboxId, String rcptEmail, long receivedDate, boolean shared, String digest, long msgSize, int folderId, boolean noICal, int flags, String[] tags, MailItem.CustomMetadata extended)
/*     */   {
/* 112 */     super(MailboxOperation.CreateMessage);
/* 113 */     setMailboxId(mailboxId);
/* 114 */     this.mRcptEmail = rcptEmail;
/* 115 */     this.mReceivedDate = receivedDate;
/* 116 */     this.mShared = shared;
/* 117 */     this.mDigest = (digest != null ? digest : "");
/* 118 */     this.mMsgSize = msgSize;
/* 119 */     this.mMsgId = 0;
/* 120 */     this.mFolderId = folderId;
/* 121 */     this.mConvId = 0;
/* 122 */     this.mConvFirstMsgId = 0;
/* 123 */     this.mMergedConvIds = Collections.emptyList();
/* 124 */     this.mFlags = flags;
/* 125 */     this.mTags = (tags != null ? tags : new String[0]);
/* 126 */     this.mMsgBodyType = 1;
/* 127 */     this.mNoICal = noICal;
/* 128 */     this.mExtendedData = extended;
/*     */   }
/*     */   
/*     */   public void start(long timestamp) {
/* 132 */     super.start(timestamp);
/* 133 */     if (this.mReceivedDate == -1L) {
/* 134 */       this.mReceivedDate = timestamp;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void commit()
/*     */   {
/*     */     try
/*     */     {
/* 153 */       super.commit();
/*     */     } finally {
/* 155 */       this.mData = null;
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void abort()
/*     */   {
/*     */     try {
/* 162 */       super.abort();
/*     */     } finally {
/* 164 */       this.mData = null;
/*     */     }
/*     */   }
/*     */   
/*     */   public int getMessageId() {
/* 169 */     return this.mMsgId;
/*     */   }
/*     */   
/*     */   public void setMessageId(int msgId) {
/* 173 */     this.mMsgId = msgId;
/*     */   }
/*     */   
/*     */   public int getConvId() {
/* 177 */     return this.mConvId;
/*     */   }
/*     */   
/*     */   public void setConvId(int convId) {
/* 181 */     this.mConvId = convId;
/*     */   }
/*     */   
/*     */   public int getConvFirstMsgId() {
/* 185 */     return this.mConvFirstMsgId;
/*     */   }
/*     */   
/*     */   public void setConvFirstMsgId(int convFirstMsgId) {
/* 189 */     this.mConvFirstMsgId = convFirstMsgId;
/*     */   }
/*     */   
/*     */   public List<Integer> getMergedConvIds() {
/* 193 */     return this.mMergedConvIds;
/*     */   }
/*     */   
/*     */   public void setMergedConvIds(List<Integer> mergedConvIds) {
/* 197 */     this.mMergedConvIds = (mergedConvIds == null ? Collections.emptyList() : mergedConvIds);
/*     */   }
/*     */   
/*     */   public void setMergedConversations(List<Conversation> mergedConvs) {
/* 201 */     if (mergedConvs == null) {
/* 202 */       this.mMergedConvIds = Collections.emptyList();
/*     */     } else {
/* 204 */       this.mMergedConvIds = Lists.newArrayList();
/* 205 */       for (Conversation conv : mergedConvs) {
/* 206 */         this.mMergedConvIds.add(Integer.valueOf(conv.getId()));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void setCalendarItemAttrs(int calItemId, int folderId)
/*     */   {
/* 213 */     this.mCalendarItemId = calItemId;
/* 214 */     this.mFolderId = folderId;
/*     */   }
/*     */   
/*     */   public int getCalendarItemId()
/*     */   {
/* 219 */     return this.mCalendarItemId;
/*     */   }
/*     */   
/*     */   public String getCalendarItemPartStat()
/*     */   {
/* 224 */     return this.mCalendarItemPartStat;
/*     */   }
/*     */   
/*     */   public void setCalendarItemPartStat(String partStat)
/*     */   {
/* 229 */     this.mCalendarItemPartStat = partStat;
/*     */   }
/*     */   
/*     */   public int getFolderId()
/*     */   {
/* 234 */     return this.mFolderId;
/*     */   }
/*     */   
/*     */   public int getFlags() {
/* 238 */     return this.mFlags;
/*     */   }
/*     */   
/*     */   public void setFlags(int flags) {
/* 242 */     this.mFlags = flags;
/*     */   }
/*     */   
/*     */   public String[] getTags() {
/* 246 */     return this.mTags;
/*     */   }
/*     */   
/*     */   public byte[] getMessageBody() throws IOException {
/* 250 */     if (this.mMsgBodyType == 2)
/* 251 */       return null;
/* 252 */     return this.mData.getData();
/*     */   }
/*     */   
/*     */   public String getPath() {
/* 256 */     return this.mPath;
/*     */   }
/*     */   
/*     */   public void setMessageBodyInfo(DataSource ds, long size) {
/* 260 */     this.mMsgBodyType = 1;
/* 261 */     this.mData = new RedoableOpData(ds, (int)size);
/* 262 */     this.mPath = ":streamed:";
/*     */   }
/*     */   
/*     */   public void setMessageBodyInfo(File dataFile) {
/* 266 */     this.mMsgBodyType = 1;
/* 267 */     this.mData = new RedoableOpData(dataFile);
/* 268 */     this.mPath = dataFile.getPath();
/*     */   }
/*     */   
/*     */   public void setMessageLinkInfo(String linkSrcPath) {
/* 272 */     this.mMsgBodyType = 2;
/* 273 */     assert (linkSrcPath != null);
/* 274 */     this.mPath = linkSrcPath;
/*     */   }
/*     */   
/*     */   public String getRcptEmail() {
/* 278 */     return this.mRcptEmail;
/*     */   }
/*     */   
/*     */   protected RedoableOpData getData() {
/* 282 */     return this.mData;
/*     */   }
/*     */   
/*     */   protected String getPrintableData() {
/* 286 */     StringBuilder sb = new StringBuilder("id=").append(this.mMsgId);
/* 287 */     sb.append(", rcpt=").append(this.mRcptEmail);
/* 288 */     sb.append(", rcvDate=").append(this.mReceivedDate);
/* 289 */     sb.append(", shared=").append(this.mShared ? "true" : "false");
/* 290 */     sb.append(", blobDigest=\"").append(this.mDigest).append("\", size=").append(this.mMsgSize);
/* 291 */     if (this.mData != null) {
/* 292 */       sb.append(", dataLen=").append(this.mData.getLength());
/*     */     }
/* 294 */     sb.append(", folder=").append(this.mFolderId);
/* 295 */     sb.append(", conv=").append(this.mConvId);
/* 296 */     sb.append(", convFirstMsgId=").append(this.mConvFirstMsgId);
/* 297 */     if ((this.mMergedConvIds != null) && (!this.mMergedConvIds.isEmpty())) {
/* 298 */       sb.append(", mergedConvIds=").append(this.mMergedConvIds);
/*     */     }
/* 300 */     if (this.mCalendarItemId != 0) {
/* 301 */       sb.append(", calItemId=").append(this.mCalendarItemId);
/*     */     }
/* 303 */     sb.append(", calItemPartStat=").append(this.mCalendarItemPartStat);
/* 304 */     sb.append(", noICal=").append(this.mNoICal);
/* 305 */     if (this.mExtendedData != null) {
/* 306 */       sb.append(", extended=").append(this.mExtendedData);
/*     */     }
/* 308 */     sb.append(", flags=").append(this.mFlags);
/* 309 */     sb.append(", tags=[").append(this.mTags == null ? "" : StringUtil.join(",", this.mTags)).append("]");
/* 310 */     sb.append(", bodyType=").append(this.mMsgBodyType);
/* 311 */     if (this.mMsgBodyType == 2) {
/* 312 */       sb.append(", linkSourcePath=").append(this.mPath);
/*     */     } else {
/* 314 */       sb.append(", path=").append(this.mPath);
/*     */     }
/* 316 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public InputStream getAdditionalDataStream() throws IOException
/*     */   {
/* 321 */     if (this.mMsgBodyType == 1) {
/* 322 */       return this.mData.getInputStream();
/*     */     }
/* 324 */     return null;
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out)
/*     */     throws IOException
/*     */   {
/* 330 */     out.writeUTF(this.mRcptEmail != null ? this.mRcptEmail : "");
/* 331 */     if (getVersion().atLeast(1, 4)) {
/* 332 */       out.writeLong(this.mReceivedDate);
/*     */     }
/* 334 */     out.writeBoolean(this.mShared);
/* 335 */     out.writeUTF(this.mDigest);
/* 336 */     out.writeLong(this.mMsgSize);
/* 337 */     out.writeInt(this.mMsgId);
/* 338 */     out.writeInt(this.mFolderId);
/* 339 */     out.writeInt(this.mConvId);
/* 340 */     if (getVersion().atLeast(1, 5))
/* 341 */       out.writeInt(this.mConvFirstMsgId);
/*     */     Iterator i$;
/* 343 */     if (getVersion().atLeast(1, 32)) {
/* 344 */       out.writeInt(this.mMergedConvIds.size());
/* 345 */       for (i$ = this.mMergedConvIds.iterator(); i$.hasNext();) { int mergeId = ((Integer)i$.next()).intValue();
/* 346 */         out.writeInt(mergeId);
/*     */       }
/*     */     }
/* 349 */     out.writeInt(this.mCalendarItemId);
/* 350 */     if (getVersion().atLeast(1, 1)) {
/* 351 */       out.writeUTF(this.mCalendarItemPartStat);
/*     */     }
/* 353 */     out.writeInt(this.mFlags);
/* 354 */     out.writeBoolean(this.mNoICal);
/* 355 */     if (getVersion().atLeast(1, 33)) {
/* 356 */       out.writeUTFArray(this.mTags);
/*     */     } else {
/* 358 */       out.writeUTF(this.mTagIds);
/*     */     }
/* 360 */     out.writeUTF(this.mPath);
/* 361 */     out.writeShort((short)-1);
/* 362 */     if (getVersion().atLeast(1, 25)) {
/* 363 */       if (this.mExtendedData == null) {
/* 364 */         out.writeUTF(null);
/*     */       } else {
/* 366 */         out.writeUTF(this.mExtendedData.getSectionKey());
/* 367 */         out.writeUTF(this.mExtendedData.getSerializedValue());
/*     */       }
/*     */     }
/*     */     
/* 371 */     out.writeByte(this.mMsgBodyType);
/* 372 */     if (this.mMsgBodyType == 1) {
/* 373 */       out.writeInt(this.mData.getLength());
/*     */ 
/*     */ 
/*     */     }
/*     */     else
/*     */     {
/*     */ 
/*     */ 
/* 381 */       out.writeShort((short)-1);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in) throws IOException
/*     */   {
/* 387 */     this.mRcptEmail = in.readUTF();
/* 388 */     if (getVersion().atLeast(1, 4)) {
/* 389 */       this.mReceivedDate = in.readLong();
/*     */     } else {
/* 391 */       this.mReceivedDate = getTimestamp();
/*     */     }
/* 393 */     this.mShared = in.readBoolean();
/* 394 */     this.mDigest = in.readUTF();
/* 395 */     if (getVersion().atLeast(1, 42)) {
/* 396 */       this.mMsgSize = in.readLong();
/*     */     } else {
/* 398 */       this.mMsgSize = in.readInt();
/*     */     }
/* 400 */     this.mMsgId = in.readInt();
/* 401 */     this.mFolderId = in.readInt();
/* 402 */     this.mConvId = in.readInt();
/* 403 */     if (getVersion().atLeast(1, 5)) {
/* 404 */       this.mConvFirstMsgId = in.readInt();
/*     */     }
/* 406 */     if (getVersion().atLeast(1, 32)) {
/* 407 */       int mergeCount = in.readInt();
/* 408 */       this.mMergedConvIds = new ArrayList(mergeCount);
/* 409 */       for (int i = 0; i < mergeCount; i++) {
/* 410 */         this.mMergedConvIds.add(Integer.valueOf(in.readInt()));
/*     */       }
/*     */     }
/* 413 */     this.mCalendarItemId = in.readInt();
/* 414 */     if (getVersion().atLeast(1, 1)) {
/* 415 */       this.mCalendarItemPartStat = in.readUTF();
/*     */     }
/* 417 */     this.mFlags = in.readInt();
/* 418 */     this.mNoICal = in.readBoolean();
/* 419 */     if (getVersion().atLeast(1, 33)) {
/* 420 */       this.mTags = in.readUTFArray();
/*     */     } else {
/* 422 */       this.mTagIds = in.readUTF();
/*     */     }
/* 424 */     this.mPath = in.readUTF();
/* 425 */     in.readShort();
/* 426 */     if (getVersion().atLeast(1, 25)) {
/* 427 */       String extendedKey = in.readUTF();
/* 428 */       if (extendedKey != null) {
/*     */         try {
/* 430 */           this.mExtendedData = new MailItem.CustomMetadata(extendedKey, in.readUTF());
/*     */         } catch (ServiceException e) {
/* 432 */           mLog.warn("could not deserialize custom metadata for message", e);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 437 */     this.mMsgBodyType = in.readByte();
/* 438 */     if (this.mMsgBodyType == 1) {
/* 439 */       int dataLength = in.readInt();
/* 440 */       boolean inMemory = false;
/*     */       try {
/* 442 */         inMemory = dataLength <= StoreManager.getDiskStreamingThreshold();
/*     */       }
/*     */       catch (ServiceException e) {}
/*     */       
/* 446 */       if (inMemory) {
/* 447 */         byte[] data = new byte[dataLength];
/* 448 */         in.readFully(data, 0, dataLength);
/* 449 */         this.mData = new RedoableOpData(data);
/*     */       } else {
/* 451 */         long pos = in.getFilePointer();
/* 452 */         this.mData = new RedoableOpData(new File(in.getPath()), pos, dataLength);
/*     */         
/*     */ 
/* 455 */         int numSkipped = in.skipBytes(dataLength);
/* 456 */         if (numSkipped != dataLength) {
/* 457 */           String msg = String.format("Attempted to skip %d bytes at position %d in %s, but actually skipped %d.", new Object[] { Integer.valueOf(dataLength), Long.valueOf(pos), in.getPath(), Integer.valueOf(numSkipped) });
/*     */           
/* 459 */           throw new IOException(msg);
/*     */         }
/*     */         
/*     */       }
/*     */     }
/*     */     else
/*     */     {
/* 466 */       in.readShort();
/*     */     }
/*     */   }
/*     */   
/*     */   DeliveryOptions getDeliveryOptions() {
/* 471 */     return new DeliveryOptions().setFolderId(this.mFolderId).setNoICal(this.mNoICal).setFlags(this.mFlags).setTags(this.mTags).setConversationId(this.mConvId).setRecipientEmail(this.mRcptEmail).setCustomMetadata(this.mExtendedData);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void redo()
/*     */     throws Exception
/*     */   {
/* 483 */     int mboxId = getMailboxId();
/* 484 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(mboxId);
/* 485 */     OperationContext octxt = getOperationContext();
/*     */     
/* 487 */     if ((this.mTags == null) && (this.mTagIds != null)) {
/* 488 */       this.mTags = TagUtil.tagIdStringToNames(mbox, octxt, this.mTagIds);
/*     */     }
/*     */     
/* 491 */     DeliveryContext dctxt = new DeliveryContext(this.mShared, Arrays.asList(new Integer[] { Integer.valueOf(mboxId) }));
/*     */     
/* 493 */     if (this.mMsgBodyType == 2) {
/* 494 */       Blob blob = StoreIncomingBlob.fetchBlob(this.mPath);
/* 495 */       if (blob == null)
/* 496 */         throw new RedoException("Missing link source blob " + this.mPath + " (digest=" + this.mDigest + ")", this);
/* 497 */       dctxt.setIncomingBlob(blob);
/*     */       
/* 499 */       ParsedMessage pm = null;
/*     */       try {
/* 501 */         ParsedMessageOptions opt = new ParsedMessageOptions().setContent(blob.getFile()).setReceivedDate(this.mReceivedDate).setAttachmentIndexing(mbox.attachmentsIndexingEnabled()).setSize(this.mMsgSize).setDigest(this.mDigest);
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 507 */         pm = new ParsedMessage(opt);
/* 508 */         mbox.addMessage(octxt, pm, getDeliveryOptions(), dctxt);
/*     */       } catch (MailServiceException e) {
/* 510 */         if (e.getCode() == "mail.ALREADY_EXISTS") {
/* 511 */           mLog.info("Message " + this.mMsgId + " is already in mailbox " + mboxId); return;
/*     */         }
/*     */         
/* 514 */         throw e;
/*     */       }
/*     */       finally {
/* 517 */         if (pm != null) {
/* 518 */           ByteUtil.closeStream(pm.getBlobInputStream());
/*     */         }
/*     */       }
/*     */     }
/*     */     else {
/* 523 */       InputStream in = null;
/*     */       try {
/* 525 */         in = this.mData.getInputStream();
/* 526 */         if (this.mData.getLength() != this.mMsgSize) {
/* 527 */           in = new GZIPInputStream(in);
/*     */         }
/* 529 */         mbox.addMessage(octxt, in, this.mMsgSize, Long.valueOf(this.mReceivedDate), getDeliveryOptions(), dctxt);
/*     */       } catch (MailServiceException e) {
/* 531 */         if (e.getCode() == "mail.ALREADY_EXISTS") {
/* 532 */           mLog.info("Message " + this.mMsgId + " is already in mailbox " + mboxId);
/*     */         }
/*     */         else {
/* 535 */           throw e;
/*     */         }
/*     */       } finally {
/* 538 */         ByteUtil.closeStream(in);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/CreateMessage.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */